Dashboard Temp Share Shortlinks Frames API

HTMLify

227. Basic Calculator II.java
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 227. Basic Calculator II

class Solution {
    public int calculate(String s) {
        Stack<Integer> operand = new Stack<>();
        Stack<Character> operator = new Stack<>();
        int i = 0;
        while(i<s.length()){
            char ch = s.charAt(i);
             
            if(ch>='0' && ch<='9'){
                int num = 0;
                while(i<s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9'){
                    num = num*10 + (s.charAt(i)-'0');
                    i++;
                }
                operand.push(num);
                i--;
            } else if(ch=='+' || ch == '-' ||ch == '*' || ch == '/') {
                while(operator.size() > 0 && prec(operator.peek()) >= prec(ch)){
                    char oper = operator.pop();
                    int val2 = operand.pop();
                    int val1 = operand.pop();
                    int cal = calc(val1 , val2 , oper);
                    operand.push(cal);
                }
                operator.push(ch);
            }
            i++;
        }
              while(operator.size() > 0){
                    char oper = operator.pop();
                    int val2 = operand.pop();
                    int val1 = operand.pop();
                    int cal = calc(val1 , val2 , oper);
                    operand.push(cal);
                }
        return operand.pop();
        
    }
    public int calc(int a , int b , char c){
        if(c=='+') return a+b;
        else if(c == '-') return a-b;
        else if(c == '*') return a * b;
        return a/b;
    }
    public int prec(char c){
        if(c=='*' || c == '/') return 1;
        else return 0;
    }
    
    
}